package net.seabookchen.pojo;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Id;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

import net.seabookchen.pojo.jaxb.BlogCommentJAXB;
import net.seabookchen.pojo.jaxb.BlogTagJAXB;
import net.seabookchen.util.DateUtils;

import org.datanucleus.util.StringUtils;
import org.jsoup.Jsoup;

import com.googlecode.objectify.Key;

@XmlRootElement(name = "post")
public class BlogPost {
	private @Id
	Long id;
	private String title;
	private String content;
	private int thumbsUp;
	private int thumbsDown;
	private List<Key<BlogComment>> comments = new ArrayList<Key<BlogComment>>();
	private List<BlogCommentJAXB> commentsJAXB = new ArrayList<BlogCommentJAXB>();
	private Date postedDate;
	private Date modifiedDate;
	private Set<Key<BlogTag>> tags = new HashSet<Key<BlogTag>>();
	private List<BlogTagJAXB> tagsJAXB = new ArrayList<BlogTagJAXB>();

	public BlogPost() {
	}

	public BlogPost(String title, String content) {
		this.title = title;
		this.content = content;
		this.thumbsUp = 0;
		this.thumbsDown = 0;
		this.postedDate = new Date();
		this.modifiedDate = new Date();
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public int getThumbsUp() {
		return thumbsUp;
	}

	public void setThumbsUp(int thumbsUp) {
		this.thumbsUp = thumbsUp;
	}

	public int getThumbsDown() {
		return thumbsDown;
	}

	public void setThumbsDown(int thumbsDown) {
		this.thumbsDown = thumbsDown;
	}

	public List<Key<BlogComment>> getComments() {
		return comments;
	}

	public void setComments(List<Key<BlogComment>> comments) {
		this.comments = comments;
	}

	public Date getPostedDate() {
		return postedDate;
	}

	public void setPostedDate(Date postedDate) {
		this.postedDate = postedDate;
	}
	
	public String getFormattedModifiedDate() {
		return DateUtils.formatDate(modifiedDate);
	}

	public Date getModifiedDate() {
		return modifiedDate;
	}

	public void setModifiedDate(Date modifiedDate) {
		this.modifiedDate = modifiedDate;
	}

	public Set<Key<BlogTag>> getTags() {
		return tags;
	}

	public void setTags(Set<Key<BlogTag>> tags) {
		this.tags = tags;
	}

	@XmlElementWrapper(name="commentsJAXB")
	@XmlElement(name="commentJAXB")
	public List<BlogCommentJAXB> getCommentsJAXB() {
		if (comments.size() > 0) {
			for (Key<BlogComment> comment : comments) {
				commentsJAXB.add(new BlogCommentJAXB(comment.getId()));
			}
		}

		return commentsJAXB;
	}
	
	@XmlElementWrapper(name="tagsJAXB")
	@XmlElement(name="tagJAXB")
	public List<BlogTagJAXB> getTagsJAXB() {
		if (tags.size() > 0) {
			for (Key<BlogTag> tag : tags) {
				tagsJAXB.add(new BlogTagJAXB(tag.getId()));
			}
		}

		return tagsJAXB;
	}

	public String getPreview() {
		if (StringUtils.isEmpty(this.content)) {
			return "";
		}
		String preview = Jsoup.parse(content).text();

		if (preview.length() > 200) {
			return preview.substring(0, 200);
		} else
			return preview;
	}

	@Override
	public String toString() {
		return "BlogPost [id=" + id + ", title=" + title + ", content="
				+ content + ", thumbsUp=" + thumbsUp + ", thumbsDown="
				+ thumbsDown + ", comments=" + comments + ", postedDate="
				+ DateUtils.formatDate(postedDate) + ", modifiedDate="
				+ DateUtils.formatDate(modifiedDate) + ", tags=" + tags
				+ "]";
	}

}
